package com.hualan.sfzy.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice //@ControllerAdvice将当前类标识为异常处理的组件
public class GlobalExceptionHandler {

    //@ExceptionHandler用于设置所标识方法处理的异常
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Map<String, Object> handleCustomException(Exception e) { //e表示当前请求处
        Map<String, Object> map = new HashMap<>();
        map.put("code", -100);
        map.put("message", e.getMessage());
        map.put("data", "Error");
        return map;
    }
}